<?php
/**
 * 产品操作类
 * Created		: 2011-12-03
 * Modified		: 2011-12-03
 * @version	: 0.1.0
 * @author		: Joseph Chen (jsph.chen@gmail.com)
 */
class Product
{

	/**
	 * 表名
	 * @var string
	 */
	const TBL	= 'product';

	/**
	 * 产品类别表名
	 * @var string
	 */
	const TBL_CATEGORY	= 'product_category';
	
	/**
	 * 每页显示几条记录
	 * @var int
	 */
	public static $pageSize = 30;
	
	/**
	 * 分页条代码
	 * @var string
	 */
	public static $pagePanel = '';
	
	
	/**
	 * 根据主键ID读取一条记录
	 * @param int $id
	 */
	public static function read($id) 
	{
		return Db::read($GLOBALS['tbl_pre'].self::TBL, (int)$id);
	}
	
	/**
	 * 获取列表
	 * @param int $cid
	 * @param string $lang
	 * @param int $pageSize
	 * @param boolean $hasPages
	 * @param string $where
	 */
	public static function getList($cid=null, $lang='zh-cn', $pageSize=30, $hasPages=true, $where='') 
	{
        empty($pageSize) && $pageSize = self::$pageSize;
        
    	if (empty($_GET['keyword']))
    	{
    		$_whr = array();
    		$params = array();
		} else {
			$_whr = array('name like :name');
			$params['name'] = '%'.$kw.'%';
		}
		
		if ($where) {
			$_whr[] = $where;
		}
		
        //关键字查询
        if ($_whr)
        {
	        $where = join(' and ', $_whr);
        } else {
        	$where = '';
        }
        $rows = Db::getCounts($GLOBALS['tbl_pre'].self::TBL, $where, $params);
        if (!$rows && !is_null(Db::errors())) {
        	App::msg(Db::$errors[2]);
			return false;
        }
		if ($hasPages)
		{
			Page::init($pageSize, $rows);
			Page::setFromTo($pageSize);
			$pageStart = Page::$from;
		} else {
			$hasPages = false;
			$pageStart = 0;
		}
        $list = Db::select(
     					$GLOBALS['tbl_pre'].self::TBL, 
     					$where, 
     					'*', 
     					$params, 
     					'', 
     					$pageStart, 
     					$pageSize
    	);
		//如果列表有分页,则进行分页操作
        if ($hasPages == 1)
		{
			self::$pagePanel = Page::generate($_POST);
		}
        return $list;
	}
	
	/**
	 * 添加一条产品
	 * @param string $content
	 * @param string $lang
	 * @param string $sign
	 */
	public static function add($cid, $picfile, $name, $content, $recommend=0, $lang='zh-cn') 
	{
		$cid = (int)$cid;
		$now = time();
		
		$cname = Db::getOne($GLOBALS['tbl_pre'].self::TBL_CATEGORY, 'id='.$cid, 'name');
		
		$data = array(
			'cid'			=> $cid,
			'cname'			=> $cname,
			'picfile'		=> $picfile,
			'name'			=> $name,
			'content'		=> $content,
			'lang'			=> $lang,
			'recommend'		=> $recommend,
			'create_time'	=> $now,
			'update_time'	=> $now,
		);
		$ret = Db::insert($GLOBALS['tbl_pre'].self::TBL, $data);
		return $ret;
	}
	
	/**
	 * 修改一条产品
	 * @param unknown_type $id
	 * @param unknown_type $cid
	 * @param string $name
	 * @param string $content
	 * @param string $lang
	 * @param boolean|string $updateTime
	 */
	public static function modify($id, $cid, $picfile, $name, $content, $recommend=0, $lang='zh-cn', $updateTime=true) 
	{
		$cid = (int)$cid;
		$now = time();
		
		$cname = Db::getOne($GLOBALS['tbl_pre'].self::TBL_CATEGORY, 'id='.$cid, 'name');
		
		$data = array(
			'cid'			=> $cid,
			'cname'			=> $cname,
			'name'			=> $name,
			'content'		=> $content,
			'lang'			=> $lang,
			'recommend'		=> $recommend,
			'create_time'	=> $now,
		);
		if ($picfile) {
			$data['picfile'] = $picfile;
		}
		if ($updateTime === true) {
			$data['update_time'] = $now;
		} else if ($updateTime === false) {
			// do nothing
		} else {
			$data['update_time'] = $now;
		}
		$ret = Db::update($GLOBALS['tbl_pre'].self::TBL, $data, 'id='.(int)$id);
		return $ret;
	}
	
	/**
	 * 删除一条产品
	 * @param int $id
	 */
	public static function delete($id) 
	{
		$ret = Db::delete($GLOBALS['tbl_pre'].self::TBL, 'id='.(int)$id);
		return $ret;
	}
	
	/**
	 * 读取一条分类信息
	 * @param int $id
	 */
	public static function readCategory($id) 
	{
		return Db::read($GLOBALS['tbl_pre'].self::TBL_CATEGORY, (int)$id);
	}
	
	/**
	 * 添加一条产品分类
	 * @param string $name 分类名称
	 * @param string $lang 所属语言版本
	 */
	public static function addCategory($name, $lang='zh-cn') 
	{
		$where = 'name=?';
		$param = array($name);
		$exist = Db::getOne($GLOBALS['tbl_pre'].self::TBL_CATEGORY, $where, 'id', $param);
		if ($exist) {
			App::msg($GLOBALS['LSameProductCategoryIsExist']);
			return false;
		}
		$data = array(
			'name'	=> $name,
			'lang'	=> $lang,
		);
		$ret = Db::insert($GLOBALS['tbl_pre'].self::TBL_CATEGORY, $data);
		return $ret;
	}
	
	/**
	 * 修改一条产品分类
	 * @param string $name 分类名称
	 * @param string $lang 所属语言版本
	 */
	public static function modifyCategory($id, $name, $lang) 
	{
		$id = (int)$id;
		$where = 'id<>'.$id.' and name=?';
		$param = array($name);
		$exist = Db::getOne($GLOBALS['tbl_pre'].self::TBL_CATEGORY, $where, 'id', $param);
		if ($exist) {
			App::msg($GLOBALS['LSameProductCategoryIsExist']);
			return false;
		}
		$where = 'name=?';
		$param = array($name);
		$exist = Db::getOne($GLOBALS['tbl_pre'].self::TBL_CATEGORY);
		$data = array(
			'name'	=> $name,
			'lang'	=> $lang,
		);
		$ret = Db::update($GLOBALS['tbl_pre'].self::TBL_CATEGORY, $data, 'id='.$id);
		return $ret;
	}
	
	/**
	 * 删除一条产品分类
	 * @param int $id
	 */
	public static function deleteCategory($id) 
	{
		$exist = Db::getOne($GLOBALS['tbl_pre'].self::TBL, 'cid='.(int)$id, 'id');
		if ($exist) {
			App::msg($GLOBALS['LDeleteCategoryNeedNoProduct']);
			return false;
		}
		$ret = Db::delete($GLOBALS['tbl_pre'].self::TBL_CATEGORY, 'id='.(int)$id);
		if (!$ret) {
			App::msg($GLOBALS['LFailure']);
		}
		return $ret;
	}
	
	/**
	 * 获取产品分类列表
	 * @param 
	 */
	public static function getCategoryList($lang=null, $hasPages=true)
	{
		if (empty($lang)) {
			$where = '';
			$params = null;
		} else {
			$where = 'lang=?';
			$params = array($lang);
		}
        $rows = Db::getCounts($GLOBALS['tbl_pre'].self::TBL_CATEGORY, $where, $params);
        if (!$rows && !is_null(Db::errors())) {
        	App::msg(Db::$errors[2]);
			return false;
        }
		if ($hasPages)
		{
			Page::init(self::$pageSize, $rows);
			Page::setFromTo(self::$pageSize);
			$pageStart = Page::$from;
		} else {
			$hasPages = false;
			$pageStart = 0;
		}
        $list = Db::select(
     					$GLOBALS['tbl_pre'].self::TBL_CATEGORY, 
     					$where, 
     					'*', 
     					$params, 
     					'', 
     					$pageStart, 
     					self::$pageSize
    	);
		//如果列表有分页,则进行分页操作
        if ($hasPages == 1)
		{
			self::$pagePanel = Page::generate($_POST);
		}
        return $list;
	}
}













